package com.liu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.entity.ChinaTotal;
import com.liu.entity.LineTrend;
import com.liu.entity.NocvData;
import com.liu.service.ChinaTotalService;
import com.liu.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;

    @Autowired
    private ChinaTotalService chinaTotalService;

    /**
     * 查询chinaTotal数据 一条最新的
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String index(Model model) throws ParseException {
        //1、找到ID最大的那一条数据
        Integer id = chinaTotalService.maxID();

        //2、根据ID进行查找数据
        ChinaTotal chinaTotal = chinaTotalService.getById(id);

        model.addAttribute("chinaTotal",chinaTotal);
        return "index";
    }

    @RequestMapping("/query")
    @ResponseBody
    public List<NocvData> queryDate(){
        List<NocvData> list = indexService.list();
        return list;
    }

    //跳转pie页面
    @RequestMapping("/toPie")
    public String toPie(){
        return "pie";
    }

    /**
     * 分组聚合
     * select count(*) from goods group by type;
     */

    @RequestMapping("queryPie")
    @ResponseBody
    public List<NocvData> queryPieData(){
        List<NocvData> list = indexService.list();
        return list;
    }

    @RequestMapping("/toBar")
    public String toBar(){
        return "bar";
    }

    @RequestMapping("queryBar")
    @ResponseBody
    public Map<String, List<Object>> queryBarData(){
        //1、所有城市数据：数值
        List<NocvData> list = indexService.list();

        //2、所有城市为数据
        List<String> cityList = new ArrayList<>();
        for (NocvData data : list) {
            cityList.add(data.getName());
        }
        //3、所有疫情数值数据
        List<Integer> dataList = new ArrayList<>();
        for (NocvData data : list) {
            dataList.add(data.getValue());
        }

        //创建map
        Map map = new HashMap<>();
        map.put("cityList", cityList);
        map.put("dataList", dataList);

        return map;
    }

    @RequestMapping("/toLine")
    public String toLine(){
        return "line";
    }

    @RequestMapping("/queryLine")
    @ResponseBody
    public Map<String,List<Object>> queryLineData(){
        //1、查询七天所有的数据
        List<LineTrend> list7Day = indexService.findSevenData();

        //2、封装所有的确诊人数
        List<Integer> confirmList = new ArrayList<>();
        //3、封装所有的隔离人数
        List<Integer> isolationList = new ArrayList<>();
        //4、封装所有的治愈人数
        List<Integer> cureList = new ArrayList<>();
        //5、封装所有的死亡人数
        List<Integer> deadList = new ArrayList<>();
        //6、封装所有的疑似人数
        List<Integer> similarList = new ArrayList<>();

        for (LineTrend data : list7Day) {
            confirmList.add(data.getConfirm());
            isolationList.add(data.getIsolation());
            cureList.add(data.getCure());
            deadList.add(data.getDead());
            similarList.add(data.getSimilar());
        }

        //创建map
        Map map = new HashMap<>();
        map.put("confirmList", confirmList);
        map.put("isolationList", isolationList);
        map.put("cureList",cureList);
        map.put("DeadList",deadList);
        map.put("similarList",similarList);

        return map;
    }
}
